package net.ikenway.springboot.demo.Controller;

import net.ikenway.springboot.demo.Model.Dto.ProductDto;
import net.ikenway.springboot.demo.Model.Dto.ResponseBodyDto;
import net.ikenway.springboot.demo.Service.Implement.ProductServiceImpl;
import net.ikenway.springboot.demo.Utils.ValidateErrorMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/18/2020
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {
    private final ProductServiceImpl productService;

    @Autowired
    ProductController(ProductServiceImpl productService) {
        this.productService = productService;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResponseBodyDto findAll(@RequestParam(required = false) String sortName) {
        Map<String, Object> data = new HashMap<>();
        List<ProductDto> productDtos;
        try {
            if (sortName != null)
                productDtos = productService.findBySortName(sortName);
            else
                productDtos = productService.findAll();
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "该分类不存在");
        } catch (Exception exception) {
            return new ResponseBodyDto(500, exception.getMessage());
        }
        return new ResponseBodyDto(200, "查询成功", productDtos);
    }

    @RequestMapping(value = "/findAllByDeleted", method = RequestMethod.GET)
    public ResponseBodyDto findAllByDeleted(@RequestParam Boolean deleted) {
        List<ProductDto> productDtos;
        try {
            productDtos = productService.findAllByDeleted(deleted);
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "查询成功", productDtos);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseBodyDto create(@Valid @RequestBody @NotNull ProductDto productDto, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors())
                throw new Exception(ValidateErrorMsgUtil.getFirstFormatMsg(bindingResult.getAllErrors()));
            productService.create(productDto);
        } catch (EntityExistsException entityExistsException) {
            return new ResponseBodyDto(500, "商品名已存在");
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "该分类不存在");
        } catch (Exception exception) {
            return new ResponseBodyDto(500, exception.getMessage());
        }
        return new ResponseBodyDto(200, "创建产品成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBodyDto update(@RequestBody ProductDto productDto) {
        try {
            if (productDto.getProductId() == null)
                throw new Exception("产品Id不能为空");
            productService.update(productDto);
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "商品不存在");
        } catch (Exception exception) {
            return new ResponseBodyDto(500, exception.getMessage());
        }
        return new ResponseBodyDto(200, "更新产品信息成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseBodyDto delete(@RequestBody Map<String, List<Long>> requestData) {
        try {
            List<Long> productIds = requestData.get("productIds");
            if (productIds == null || productIds.isEmpty()) throw new Exception("产品ID不能为空");
            productService.delete(productIds);
        } catch (Exception exception) {
            return new ResponseBodyDto(500, exception.getMessage());
        }

        return new ResponseBodyDto(200, "删除成功");
    }
}
